package servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import edu.mta.pokerstats.Game;
import edu.mta.pokerstats.GameRecord;

/**
 * Servlet implementation class servletGame
 */
public class servletGame extends BaseRecordsAbstractServlet {
	private static final long serialVersionUID = 1L;
	private static final String k_TotalPotPrefix = "\"totalPot\" : ";
	private static final String k_TotalWinningsPrefix = "\"totalWinnings\" : ";
	private static final String k_numPlayersPrefix = "\"numPlayers\" : ";
	private static final String k_TipPrefix = "\"tip\" : ";
	private SimpleDateFormat m_DateFormatter = new SimpleDateFormat("yyyy-MM-dd");

	protected void processInsertRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		int gameID = 0;
		Game currGame;
		int teamID = Integer.parseInt(request.getParameter("teamID"));
		Date gameDate;
		int gameBuyIn;
		int gameRebuy;
		String gameRemark;
		int totalPot = 0;
		int totalWinnings = 0;
		int numPlayers = 0;
		int tip;
		
		try {
			gameDate = new Date(m_DateFormatter.parse(
					request.getParameter("gameDate")).getTime());
		} catch (ParseException e) {
			gameDate = new Date(new java.util.Date().getTime());
			e.printStackTrace();
		}
		gameBuyIn = Integer.parseInt(request.getParameter("gameBuyIn"));
		gameRebuy = Integer.parseInt(request.getParameter("gameRebuy"));
		gameRemark = request.getParameter("gameRemark");
		currGame = new Game(gameID, teamID, gameDate, gameBuyIn, gameRebuy, gameRemark);
		if (currGame.InsertToDatabase()) {
			ArrayList<GameRecord> recordsForGame = currGame.GetRecordsForGame();
			for (GameRecord currGR : recordsForGame) {
				totalPot += currGame.GetInvestment(currGR.getNumberOfRebuys());
				totalWinnings += currGR.getWonInGame();
				numPlayers++;
			}
			tip = totalPot - totalWinnings;
			out.print("{" + k_SuccessStr + ","
					+ "\"gameID\" : " + "\"" + currGame.getId() + "\","
					+ k_TotalPotPrefix + "\"" + totalPot + "\","
					+ k_TotalWinningsPrefix + "\"" + totalWinnings
					+ "\"," + k_numPlayersPrefix + "\"" + numPlayers
					+ "\"," + k_TipPrefix + "\"" + tip + "\"}");
		} else {
			out.print("{" + k_FailStr + "}");
		}
	}

	protected void processUpdateRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		int gameID = 0;
		Game currGame;
		gameID = Integer.parseInt(request.getParameter("gameID"));
		currGame = Game.getGameById(gameID);
		Date gameDate;
		int gameBuyIn;
		int gameRebuy;
		String gameRemark;
		int totalPot = 0;
		int totalWinnings = 0;
		int numPlayers = 0;
		int tip;
		
		try {
			gameDate = new Date(m_DateFormatter.parse(
					request.getParameter("gameDate")).getTime());
		} catch (ParseException e) {
			gameDate = new Date(new java.util.Date().getTime());
			e.printStackTrace();
		}
		currGame.setDate(gameDate);
		gameBuyIn = Integer.parseInt(request.getParameter("gameBuyIn"));
		currGame.setCostOfBuyIn(gameBuyIn);
		gameRebuy = Integer.parseInt(request.getParameter("gameRebuy"));
		currGame.setCostOfRebuy(gameRebuy);
		gameRemark = request.getParameter("gameRemark");
		currGame.setRemark(gameRemark);
		if (currGame.UpdateInDatabase()) {
			ArrayList<GameRecord> recordsForGame = currGame.GetRecordsForGame();
			for (GameRecord currGR : recordsForGame) {
				totalPot += currGame.GetInvestment(currGR.getNumberOfRebuys());
				totalWinnings += currGR.getWonInGame();
				numPlayers++;
			}
			tip = totalPot - totalWinnings;
			out.print("{ " + k_SuccessStr + "," + k_TotalPotPrefix + "\""
					+ totalPot + "\"," + k_TotalWinningsPrefix + "\""
					+ totalWinnings + "\"," + k_numPlayersPrefix + "\""
					+ numPlayers + "\"," + k_TipPrefix + "\"" + tip + "\"}");

		} else {
			out.print("{" + k_FailStr + "}");
		}
	}

	protected void processDeleteRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		int gameID = 0;
		Game currGame;
		
		gameID = Integer.parseInt(request.getParameter("gameID"));
		currGame = Game.getGameById(gameID);
		if (currGame.DeleteFromDatabase()) {
			ArrayList<GameRecord> recordsForGame = currGame.GetRecordsForGame();
			for (GameRecord gameRecord : recordsForGame) {
				gameRecord.DeleteFromDatabase();
			}
			out.print("{" + k_SuccessStr + "}");
		} else {
			out.print("{" + k_FailStr + "}");
		}
	}
}